package com.baoyouqun.service.impl;

import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baoyouqun.base.utils.ConvertUtils;
import com.baoyouqun.domain.CreateCmd.InvoiceOrderRelCreateCmd;
import com.baoyouqun.domain.Query.InvoiceOrderRelQuery;
import com.baoyouqun.domain.UpdateCmd.InvoiceOrderRelUpdateCmd;
import com.baoyouqun.domain.VO.InvoiceOrderRelVO;
import com.baoyouqun.entity.InvoiceOrderRel;
import com.baoyouqun.mapper.InvoiceOrderRelMapper;
import com.baoyouqun.service.InvoiceOrderRelService;
import jakarta.annotation.Resource;
import org.apache.poi.util.StringUtil;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;


@Service
public class InvoiceOrderRelServiceImpl extends ServiceImpl<InvoiceOrderRelMapper, InvoiceOrderRel> implements InvoiceOrderRelService {

    @Resource
    private InvoiceOrderRelMapper invoiceOrderRelMapper;

    @Override
    public SingleResponse<InvoiceOrderRelVO> create(InvoiceOrderRelCreateCmd cmd) {
        InvoiceOrderRel entity =
                ConvertUtils.copyBean(cmd, InvoiceOrderRel.class);
        Boolean result = save(entity);
        if (!result) {
            return SingleResponse.buildFailure("100101", "创建失败");
        }

        InvoiceOrderRelVO vo =
                ConvertUtils.copyBean(entity, InvoiceOrderRelVO.class);
        return SingleResponse.of(vo);
    }

    @Override
    public Response update(InvoiceOrderRelUpdateCmd cmd) {
        if (Objects.isNull(cmd)) {
            return Response.buildFailure("100301", "参数不能为空");
        }
        if (StringUtil.isBlank(cmd.getId())) {
            return Response.buildFailure("100302", "ID不能为空");
        }
        InvoiceOrderRel oldEntity = getById(cmd.getId());
        if (Objects.isNull(oldEntity)) {
            return Response.buildFailure("100303", "未查询到此数据");
        }
        oldEntity = ConvertUtils.copyBean(cmd, InvoiceOrderRel.class);
        Boolean result = updateById(oldEntity);
        if (result) {
            return Response.buildSuccess();
        } else {
            return Response.buildFailure("100304", "更新失败");
        }
    }

    @Override
    public InvoiceOrderRelVO selectById(String id) {
        InvoiceOrderRel entity = invoiceOrderRelMapper.selectById(id);
        InvoiceOrderRelVO vo =
                ConvertUtils.copyBean(entity, InvoiceOrderRelVO.class);
        return vo;
    }

    @Override
    public IPage<InvoiceOrderRelVO> pageVo(InvoiceOrderRelQuery query) {
        IPage<InvoiceOrderRel> page = new Page<InvoiceOrderRel>(query.getCurrent(), query.getSize());
        page = page(page, new LambdaQueryWrapper<InvoiceOrderRel>());
        Page<InvoiceOrderRelVO> voPage = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
        List<InvoiceOrderRelVO> records = ConvertUtils.copyBeanList(page.getRecords(), InvoiceOrderRelVO.class);
        voPage.setRecords(records);
        return voPage;
    }
}